import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { SectionService } from '../../../services/section.service';
import { aroAnimation } from '../../../animation';

@Component({
  selector: 'app-home-section',
  templateUrl: './home-section.component.html',
  styleUrls: ['./home-section.component.css'],
  animations: [aroAnimation]
})
export class HomeSectionComponent implements OnInit {

  public sections: Array<any> = JSON.parse(localStorage.getItem('tempSections')) || [];
  public tempSections: Array<any> = [];
  // public section: Array<any> = [];

  constructor(private sectionService: SectionService, private router: Router) { }

  ngOnInit() {
    this.sectionService.getList().subscribe(res => {
      // console.log(res);
      if (res.Code == 100) {
        this.tempSections = res.Data;
        // 存入session
        localStorage.setItem('tempSections', JSON.stringify(this.tempSections));
        let localSectionData = JSON.parse(localStorage.getItem('tempSections'));
        // console.log(localSectionData);
        for (let i = 0; i < localSectionData.length; i++) {
          // console.log(this.tempSections[i].Books.length)
          if (localSectionData[i].Books.length) {
            this.sections.push(localSectionData[i]);
            //截取四本放到页面
            this.sections[i].Books = this.sections[i].Books.slice(0, 4);
          }
        }
        // console.log(this.sections);
      }
    });
  }

  public goDetail(book): void {
    // console.log(book);
    this.router.navigate(['./bookdetail/', book.Book.Id]);
  }

  public goList(item): void {
    // console.log(item);
    this.router.navigate(['./homelist/', item.Id, item.Name]);
  }

}
